#!/usr/bin/python
# coding:utf-8
__author__ = 'good'
__createday__ = '2015-01-15'

import flask
from flask import g
import flask_login

from ..extensions import lm, db
from .. import models
from ..helpers import blog_render
from . import forms
from ..tasks import register_mail
from ..helpers import make_random_passwd


@lm.user_loader
def load_user(uid):
    return models.User.query.get(int(uid))


# ：登陆
def login():
    if g.user is not None and g.user.is_authenticated():
        return flask.redirect(flask.url_for('main.index'))
    form = forms.LoginForm()
    if form.validate_on_submit() and flask.request.method == 'POST':
        user = models.User.query.filter_by(email=form.email.data).first_or_404()
        remember_me = form.remember_me.data
        if user.check_password(form.passwd.data):
            flask_login.login_user(user, remember=remember_me)
            flask.flash(u'恭喜，登录成功！')
            log = models.Login_log(email=user.email,
                                   ip=flask.request.remote_addr)
            db.session.add(log)
            db.session.commit()
            models.Blog_info.new_login()
            return flask.redirect(flask.request.args.get("next") or flask.url_for("main.index"))
        else:
            flask.flash(u'用户名或密码错误')
            return flask.redirect(flask.url_for('user.login'))
    return blog_render('user/login.html',
                       title=u'请登陆',
                       form=form)


# ：登出
@flask_login.login_required
def logout():
    flask_login.logout_user()
    flask.flash(u'已退出登录')
    return flask.redirect(flask.url_for('main.index'))


# ：用户信息编辑
@flask_login.login_required
def usereditinfo():
    form = forms.UserEditForm()
    if form.validate_on_submit() and flask.request.method == 'POST':
        g.user.nicename = form.nicename.data
        g.user.info = form.info.data
        g.user.url = form.url.data
        db.session.add(g.user)
        db.session.commit()
        flask.flash(u'已保存修改！')
        return flask.redirect(flask.url_for('user.usereditinfo'))
    else:
        form.nicename.data = g.user.nicename
        form.info.data = g.user.info
        form.url.data = g.user.url
    return blog_render('user/usereditinfo.html',
                       title=u'修改用户信息',
                       form=form)


# ：用户密码更改
def userchangepwd():
    form = forms.UserChangePwdForm()
    if form.validate_on_submit() and flask.request.method == 'POST':
        g.user.set_password(form.password.data)
        db.session.add(g.user)
        db.session.commit()
        flask.flash(u'密码修改成功！')
        return flask.redirect(flask.url_for('user.usereditinfo'))
    return blog_render('user/userchangepwd.html',
                       title=u'修改密码',
                       form=form)


# ：用户注册
def register():
    form = forms.RegisterForm(flask.request.form)
    if flask.request.method == 'POST' and form.validate():
        pwd = make_random_passwd()
        user = models.User(email=form.email.data,
                           nicename=form.email.data,
                           register_ip=flask.request.remote_addr,
                           )
        user.set_password(pwd)
        db.session.add(user)
        db.session.commit()
        user.realpwd = pwd
        register_mail(user)
        flask.flash(u'恭喜，注册成功！')
        models.Blog_info.new_user()
        return flask.redirect(flask.url_for('user.login'))
    return blog_render('user/register.html',
                       title=u'欢迎注册',
                       form=form)
